package com.lfy.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.lfy.annotaion;
import com.lfy.aop.CheckJWT;
import com.lfy.dao.LoginDao;
import com.lfy.domian.User;
import com.lfy.service.Crud;
import com.lfy.service.Fun;
import com.lfy.service.TestService;
import com.lfy.util.R;
import lombok.SneakyThrows;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@RestController

public class LoginController {
    @Resource
    private LoginDao loginDao;
    @Resource
    private TestService testService;
    @Resource
    private Crud crud;
   @Resource
   private Fun fun;

    @PostMapping("/test")
    public String LoginCheck( ){

        new ReentrantLock();
        try {
            testService.test();
        } catch (Exception e) {
            return "error";
        }
        return "ok";
    }

    @GetMapping("/test")

    public String test( ){

        List<User> gest = loginDao.gest();
//        Map<String, List<User>> collect = gest.stream().collect(Collectors.groupingBy(item -> item.getEmail()));
//
//        System.out.println(collect);

        return "ok";
    }
    @GetMapping("/login")

    public R login(){
//        String uid = "1";
        StpUtil.login(1);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return R.SUCCESS(tokenInfo);
    }
    @GetMapping("/islogin")

    public R islogin(){
        boolean login = StpUtil.isLogin();
        return R.SUCCESS(login);
    }

    @GetMapping("/loginout")

    public R logout(){
        StpUtil.logout(1);
        return R.SUCCESS();
    }
    @GetMapping("tran")
    public String tran(){



            crud.insertUser();


        return "ok";
    }
    @GetMapping("asd")
    @annotaion(name = "吕飞扬",go =2 )
    public String asd(){
        System.out.println("舒服啊");
        fun.ts(()->{
            System.out.println("操了");
            return 1;
        });
        return "ok";
    }
    @SneakyThrows
    @GetMapping("/down")
    @CrossOrigin(origins = "*")
    public void down(HttpServletResponse response){

            // path是指欲下载的文件的路径。
            File file = ResourceUtils.getFile("classpath:test.docx");
            // 取得文件名。
            // 下载本地文件
            // 读到流中
            InputStream inStream = new FileInputStream(file);// 文件的存放路径
            // 设置输出的格式
            response.reset();
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition","attachment;filename="+file.getName());
        response.setCharacterEncoding("UTF-8");

            // 循环取出流中的数据
            byte[] b = new byte[100];
            int len;
            try {
                while ((len = inStream.read(b)) > 0) {
                    response.getOutputStream().write(b, 0, len);
                }
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}
